#include "analogclock.h"
#include <QPainter>//添加一个绘图工具
#include <QPoint>
#include <QTimer>//添加时钟
#include <QTime>//添加时间
#include <QColor>
#include <QBrush>
#include "configargs.h"

AnalogClock::AnalogClock(QWidget *parent)//构造函数
        : QWidget(parent)

{
    QTimer *timer = new QTimer(this);//新建一个定时器对象
    timer->start(1000);//1000代表1000ms，即1s
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    resize(150,150);
}

AnalogClock::~AnalogClock()//析构
{

}

void AnalogClock::paintEvent(QPaintEvent *)//定义重画函数
{
    QPainter painter(this);//新建一个QPainter对象
    QTime time = QTime::currentTime();//获取当前系统时间

    if(g_lcdsize == LCDSIZE_480x272)
    {
    	sed[0] = QPoint(-2,-70);
    	sed[1] = QPoint(2,-70);
    	sed[2] = QPoint(2,15);
    	sed[3] = QPoint(-2,15);

		min[0] = QPoint(-2,-50);
		min[1] = QPoint(2,-50);
		min[2] = QPoint(2,10);
		min[3] = QPoint(-2,10);

		hour[0] = QPoint(-3,-40);
		hour[1] = QPoint(3,-40);
		hour[2] = QPoint(3,5);
		hour[3] = QPoint(-3,5);
	
    }else{
       	sed[0] = QPoint(-5,-140);
    	sed[1] = QPoint(5,-140);
    	sed[2] = QPoint(5,30);
    	sed[3] = QPoint(-5,30);

		min[0] = QPoint(-5,-100);
		min[1] = QPoint(5,-100);
		min[2] = QPoint(5,20);
		min[3] = QPoint(-5,20);

		hour[0] = QPoint(-6,-80);
		hour[1] = QPoint(6,-80);
		hour[2] = QPoint(6,10);
		hour[3] = QPoint(-6,10);


    }

    painter.translate(this->width()/2, this->height()/2);//实现坐标的平移，将点（150,150）设为原点
    painter.setRenderHint(QPainter::Antialiasing);//是边缘平滑
    painter.setBrush(QBrush(QColor(30, 100, 156, 200)));//填充颜色
    painter.setPen(QColor(255, 255, 255, 190));//线的颜色
    painter.save();//保存坐标系状态

    painter.rotate(6.0*time.second());//rotate(x)以原点为中心坐标轴顺时针旋转x度
    painter.drawConvexPolygon(sed,4);//凸多边形 连接四个点
    painter.restore();//恢复以前坐标系状态
    painter.setBrush(QBrush(QColor(22, 56, 94, 200)));
    painter.setPen(QColor(255, 255, 255, 128));
    painter.save();

    painter.rotate(6.0*(time.minute()+time.second()/60.0));
    painter.drawConvexPolygon(min,4);
    painter.restore();
    painter.setBrush(QBrush(QColor(18, 34, 47, 200)));
    painter.setPen(QColor(255, 255, 255, 128));
    painter.save();

    painter.rotate(30.0*(time.hour()+(time.minute()/60.0)));
    painter.drawConvexPolygon(hour,4);
    painter.restore();
    

    painter.setPen(QColor(255, 255, 255, 128));
    painter.setBrush(QBrush(QColor(0, 0, 0, 150)));

	if(g_lcdsize == LCDSIZE_480x272)
	{
		for(int i=0; i<12; i++){
        	painter.rotate(30);
        	painter.drawRect(-4, -100, 4, 9);
    	}

	}else {
		for(int i=0; i<12; i++){
		    painter.rotate(30);
		    painter.drawRect(-4, -175, 8, 18);
		}
    }

}
